package com.android.yinghebike.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.KeyEvent;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.util.Util;
import com.android.baselibrary.view.MyBottomTabView;
import com.android.yinghebike.R;
import com.android.yinghebike.manager.UnitManager;
import com.android.yinghebike.ui.fragment.HomeMainFragment;
import com.android.yinghebike.ui.fragment.HomeMineFragment;
import com.android.yinghebike.ui.fragment.HomeNavFragment;
import com.android.yinghebike.ui.fragment.NavFragment1;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity {


    public final static int HOME_INDEX_0 = 0;
    public final static int HOME_INDEX_1 = 1;
    public final static int HOME_INDEX_2 = 2;
    public final static int HOME_INDEX_3 = 3;
    public final static int HOME_INDEX_4 = 4;

    public static int curruntIndex;

    @BindView(R.id.mybottomtabview)
    MyBottomTabView mybottomtabview;
    Fragment currentFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        initTab();
        chooseFragment(HOME_INDEX_1);
        mybottomtabview.setSelect(HOME_INDEX_1);
    }

    //设置tab参数
    private void initTab() {
        mybottomtabview.setOnTabSelectCallBack(onBottomTabSelect);
        mybottomtabview.setTextColor(Color.parseColor("#555555"), getResources().getColor(com.android.baselibrary.R.color.mainColor));
        List<String> tab_name = new ArrayList<String>();   //文字
        tab_name.add("导航");
        tab_name.add("首页");
        tab_name.add("我的");
        List<Integer> ico_normal = new ArrayList<>();       //默认图片
        ico_normal.add(R.drawable.ico_home_nav_normal);
        ico_normal.add(R.drawable.ico_home_main_normal1);
        ico_normal.add(R.drawable.ico_home_mine_normal);
        List<Integer> ico_select = new ArrayList<>();
        ico_select.add(R.drawable.ico_home_nav_select);       //选中图片
        ico_select.add(R.drawable.ico_home_main_select1);
        ico_select.add(R.drawable.ico_home_mine_select);
        mybottomtabview.setData(tab_name, ico_normal, ico_select, false, com.android.baselibrary.R.drawable.ico_home_bottom_center_);

    }

    @Override
    protected void initData() {
        //获取更新信息
        Log.i("test", "num:"+ UnitManager. getDisplaySpeedByUnit(20.1));
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    //切换Fragment
    private void chooseFragment(int index) {
        currentFragment = getSupportFragmentManager().findFragmentByTag(String.valueOf(index));
        hideAllFrag();
        if (currentFragment == null) {
            switch (index) {
                case HOME_INDEX_0:
                    currentFragment = new NavFragment1();
                    break;
                case HOME_INDEX_1:
                    currentFragment = new HomeMainFragment();
                    break;
                case HOME_INDEX_2:
                    currentFragment = new HomeMineFragment();
                    break;
            }
            addFragment(currentFragment, index);
        } else {
            showFragment(currentFragment);
        }
        //切换刷新
        currentFragment.onResume();
        curruntIndex = index;
    }

    CommCallBack onBottomTabSelect = new CommCallBack() {
        @Override
        public void onResult(Object obj) {
            int index = (int) obj;
            if (index == mybottomtabview.centerIndex) {
                //点击加号回调
                CommToast.showToast(mContext, "点击加号");
//                Intent intent = new Intent(mContext, AddTaskActivity.class);
//                startActivity(intent);


            } else {
                chooseFragment(index);
                mybottomtabview.setSelect(index);
            }
        }
    };

    private void addFragment(Fragment currentFragment, int index) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.framelayout, currentFragment, index + "");
        ft.commitAllowingStateLoss();
    }

    private void hideAllFrag() {
        for (int i = 0; i < 5; i++) {
            Fragment fragment = getSupportFragmentManager().findFragmentByTag(String.valueOf(i));
            if (fragment != null) {
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.hide(fragment);
                ft.commitAllowingStateLoss();
            }
        }
    }

    public void showFragment(Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (fragment.isDetached()) {
            ft.attach(fragment).show(fragment);
            ft.commitAllowingStateLoss();
        } else {
            ft.show(fragment);
            ft.commitAllowingStateLoss();
        }
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                CommToast.showToast(mContext, getString(R.string.toast_exit_app));
                exitTime = System.currentTimeMillis();
                return true;
            } else {
                System.exit(0);
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        currentFragment.onActivityResult(requestCode, resultCode, data);
    }
}